//
//  RegisterVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/31.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "RegisterVC.h"
#import "AppDelegate.h"
#import "UserMovieLabelVC.h"
@interface RegisterVC () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>
{
    int pIntSexSelect;
    BOOL pGetMessage_No;
    NSMutableArray *pArrayLabel;
    NSArray *pArrayPlaceholder;
    
    NSString *pStrHeadNum;
}

@property (weak, nonatomic) IBOutlet UIButton *pBtnHeadImg;
@property (weak, nonatomic) IBOutlet UISegmentedControl *pSegSex;
@property (weak, nonatomic) IBOutlet UITextField *pTxtNickName;
@property (weak, nonatomic) IBOutlet UITextField *pTxtBirthday;
@property (weak, nonatomic) IBOutlet UITextField *pTxtProfession;
@property (nonatomic,strong) UITableViewCell *curCell;
@end

@implementation RegisterVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)initInfo {
    [super initInfo];
    pStrHeadNum = @"-1";

    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    [btnBack setImage:[UIImage imageNamed:@"back2"] forState:UIControlStateNormal];
    [btnBack setTitle:@"   " forState:UIControlStateNormal];
    [btnBack.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [btnBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnBack setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [btnBack addTarget:self action:@selector(selPopOrDismissViewController) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    self.title = @"注册新用户";

    pIntSexSelect = 0;
    pArrayLabel = [NSMutableArray arrayWithObjects:@"", @"", @"", @"", @"", @"", @"", nil];
    pArrayPlaceholder = [NSArray arrayWithObjects:@"选择你的星座", @"添加你喜欢的美食", @"添加你喜欢的电影", @"添加你喜欢的演员", @"添加你喜欢的导演", @"添加你喜欢的电影类型", @"添加你常去的电影院", nil];
}

- (void)switchValueChangeAction:(UISwitch *)theSwitch {
    pGetMessage_No = !theSwitch.on;
}

- (IBAction)btnSelectHeadImgAction:(UIButton *)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"请上传您的真实照片作为头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
    
    [actionSheet showInView:self.view];
}


- (IBAction)btnResgisterAction:(UIButton *)sender
{
    if (self.pTxtNickName.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的昵称"];
        return;
    } else if (self.pTxtBirthday.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的年龄"];
        return;
    }
    
    [SVProgressHUD show];
    [TDNetwork registerPostURL:registerIP andPhoneNumber:[_pDictInfo objectForKey:@"phone"] andRandom:[_pDictInfo objectForKey:@"phoneCode"] andPwd:@"123456" andType:@"1"  andHeadImage:pStrHeadNum andNickName:self.pTxtNickName.text andUserSex:pIntSexSelect==0?@"1":@"2" andUserAge:[self.pTxtBirthday.text integerValue] andMovieLabel:@"x" andInviterPhone:@"" andMac:@"" andImsi:@"" andSystemName:@"" andSystemVersion:@"" complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            
            switch ([ret intValue]) {
                case 1:
                {
                    NSMutableArray *array = [NSMutableArray array];
                    for (MovieLabelModel *item in pArrayLabel) {
                        if ([item isKindOfClass:[MovieLabelModel class]]) {
                            [array addObject:item.uuid];
                        } else {
                            if ([item isKindOfClass:[NSArray class]]) {
                                for (MovieLabelModel *j in (NSArray *)item) {
                                    [array addObject:j.uuid];
                                }
                            } else {
                                NSString *strUUid = (NSString *)item;
                                if (strUUid.length > 0) {
                                    [array addObject:strUUid];
                                }
                            }
                        }
                    }
                    
                    [TDNetwork updateUserLabel:updateMovieLabel_interface userLabels:pArrayLabel complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
                        if (succeed) {
                            
                        }else{
                        
                        }
                    }];
                    
                    
                    NSString *identifierForVendor = [[NSUserDefaults standardUserDefaults] objectForKey:MACROS_KEY_DEVICE_TOKEN];
                    [TDNetwork loginPostURL:loginIP andphoneNumber:[_pDictInfo objectForKey:@"phone"] andPwd:@"123456" andType:@"2" andMac:@"" andImsi:@"" andSystemName:@"" andSystemVersion:@"" andDeviceToke:identifierForVendor?identifierForVendor:[_pDictInfo objectForKey:@"phone"] andDeviceType:@"2" complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
                        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                        UIViewController *tabbar = [storyBoard instantiateInitialViewController];
                        if (succeed)
                        {
                            switch ([ret intValue])
                            {
                                case 1:
                                    [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                                    [self.navigationController presentViewController:tabbar animated:NO completion:nil];
                                    break;
                                    
                                case -1:
                                    [SVProgressHUD showErrorWithStatus:@"手机号或者密码不正确"];
                                    break;
                                    
                                case -2:
                                    [SVProgressHUD showErrorWithStatus:@"账户已冻结"];
                                    break;
                                    
                                case -3:
                                    [SVProgressHUD showErrorWithStatus:@"验证次数太多，稍后再试"];
                                    break;
                                    
                                default:
                                    [SVProgressHUD showErrorWithStatus:@"未知错误"];
                                    break;
                            }
                            
                            UIApplication *app = [UIApplication sharedApplication];
                            AppDelegate *appDelegate = app.delegate;
                            [appDelegate selCloseNoti];
                        } else {
                            [SVProgressHUD showErrorWithStatus:@"网络请求失败"];
                        }
                    }];
                }
                    break;
                    
                case -2:
                    [SVProgressHUD showErrorWithStatus:@"随机数不对"];
                    break;
                    
                case -3:
                    [SVProgressHUD showErrorWithStatus:@"超时10分钟"];
                    break;
                    
                default:
                    [SVProgressHUD showErrorWithStatus:@"未知错误"];
                    break;
            }
        }else{
            [SVProgressHUD showInfoWithStatus:@"网络请求失败"];
        }
    }];
}


#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0:  //打开照相机拍照
            [self takePhoto];
            break;
            
        case 1:  //打开本地相册
            [self LocalPhoto];
            break;
    }
}


//开始拍照
-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:^{}];
    }else{
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

//打开本地相册
-(void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{}];
}

//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        NSData *data;
        data = UIImageJPEGRepresentation(image, 0.5);
        
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
        
        //得到选择后沙盒中图片的完整路径
        NSString *filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:^{}];
        //创建一个选择后图片的小图标放在下方
        NSString *encodedImageStr = [data base64Encoding];
        [SVProgressHUD show];
        [TDNetwork uploadingUserPicImagePostUrl:uploadingPicImageIP
                                        imgName:@"image.png"
                                        imgData:encodedImageStr
                                          title:@"图片"
                                           type:@"png"
                                       complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
                                           if (succeed) {
                                               switch ([ret intValue])
                                               {
                                                   case 1: {
                                                       pStrHeadNum = [NSString stringWithFormat:@"%@", [[dictionary valueForKey:@"res"] valueForKey:@"fid"]];
                                                       if (pStrHeadNum) {
                                                           [self.pBtnHeadImg setImage:image forState:UIControlStateNormal];
                                                       } else {
                                                           [SVProgressHUD showErrorWithStatus:@"上传头像失败"];
                                                       }
                                                       [SVProgressHUD dismiss];
                                                   }
                                                       break;
                                                   case 2:
                                                       [SVProgressHUD showErrorWithStatus:@"格式不对"];
                                                       break;
                                                   case 3:
                                                       [SVProgressHUD showErrorWithStatus:@"大小超限"];
                                                       break;
                                                   default:
                                                       [SVProgressHUD showErrorWithStatus:@"未知错误"];
                                                       break;
                                               }
                                           } else {
                                               [SVProgressHUD showErrorWithStatus:@"网络请求错误"];
                                           }
                                       }
         ];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{}];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section)
    {
        case 0:
            return 3;
            
        case 1:
            return 1;
            
        case 2:
            return 7;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strCell = @"";
    switch (indexPath.section) {
        case 0:
            strCell = @"idfRegisterSexCell";
            break;
            
        case 1:
            strCell = @"idfRegisterMessageCell";
            break;
            
        case 2:
            strCell = @"idfRegisterLabelCell";
            break;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
    }
    
    switch (indexPath.section) {
        case 0:{
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            
            //设置显示信息
            [self selSuperView:cell.contentView SetSubLabelOfTag:1101 andValue:indexPath.row==0?@"男生":(indexPath.row==1?@"女生":@"男生和女生")];
            UILabel *lblSex = (UILabel *)[cell.contentView viewWithTag:1101];
            UIImageView *img = (UIImageView *)[cell.contentView viewWithTag:1301];
            if (indexPath.row == pIntSexSelect) {
                img.hidden = NO;
                [lblSex setTextColor:[UIColor colorWithRed:253/255.0f green:178/255.0f blue:81/255.0f alpha:1]];
            } else {
                img.hidden = YES;
                [lblSex setTextColor:[UIColor blackColor]];
            }
        }
            break;
            
        case 1:
        {
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            UISwitch *switchMessage = (UISwitch *)[cell.contentView viewWithTag:1501];
            switchMessage.on = !pGetMessage_No;
            [switchMessage addTarget:self action:@selector(switchValueChangeAction:) forControlEvents:UIControlEventValueChanged];
        }
            break;
            
        case 2:
        {
            [self selSuperView:cell.contentView SetSubLabelOfTag:1101 andValue:indexPath.row==0?@"星          座":indexPath.row==1?@"美          食":indexPath.row==2?@"电          影":indexPath.row==3?@"演          员":indexPath.row==4?@"导          演":indexPath.row==5?@"电 影 类 型":@"常 去 影 院"];
            UITextField *txt = (UITextField *)[cell.contentView viewWithTag:1201];
            UITextField *txt1 = (UITextField *)[cell.contentView viewWithTag:1202];
            UITextField *txt2 = (UITextField *)[cell.contentView viewWithTag:1203];
            UITextField *txt3 = (UITextField *)[cell.contentView viewWithTag:1204];
            UITextField *txt4 = (UITextField *)[cell.contentView viewWithTag:1205];
            UITextField *txt5 = (UITextField *)[cell.contentView viewWithTag:1206];

            [txt1 setHidden:YES];
            [txt2 setHidden:YES];
            [txt3 setHidden:YES];
            [txt4 setHidden:YES];
            [txt5 setHidden:YES];
            
            txt.layer.masksToBounds = YES;
            txt.layer.cornerRadius = 10;
            txt1.layer.masksToBounds = YES;
            txt1.layer.cornerRadius = 10;
            txt2.layer.masksToBounds = YES;
            txt2.layer.cornerRadius = 10;
            txt3.layer.masksToBounds = YES;
            txt3.layer.cornerRadius = 10;
            txt4.layer.masksToBounds = YES;
            txt4.layer.cornerRadius = 10;
            txt5.layer.masksToBounds = YES;
            txt5.layer.cornerRadius = 10;


            switch (indexPath.row)
            {
                case 0:
                {
                    MovieLabelModel *mlm = [pArrayLabel objectAtIndex:indexPath.row];
                    if ([mlm isKindOfClass:[MovieLabelModel class]]) {
                        txt.text = [NSString stringWithFormat:@"  %@      ", mlm.name];
                    }
                    if ([mlm isKindOfClass:[MovieLabelModel class]]) {
                        txt.textColor = [UIColor colorWithRed:254.0/255 green:189.0/255 blue:1.0/255 alpha:1];
                        txt.backgroundColor = [UIColor colorWithRed:255.0/255 green:250.0/255 blue:238.0/255 alpha:1];
                    } else {
                        [txt setPlaceholder:[pArrayPlaceholder objectAtIndex:indexPath.row]];
                    }
                }
                    break;
                    
                case 1:
                {
                    id idData = [pArrayLabel objectAtIndex:indexPath.row];
                    
                    if ([idData isKindOfClass:[NSArray class]])
                    {
                        txt.textColor = [UIColor colorWithRed:254.0/255 green:144.0/255 blue:57.0/255 alpha:1];
                        txt.backgroundColor = [UIColor colorWithRed:255.0/255 green:246.0/255 blue:239.0/255 alpha:1];
                        txt1.textColor = [UIColor colorWithRed:254.0/255 green:144.0/255 blue:57.0/255 alpha:1];
                        txt1.backgroundColor = [UIColor colorWithRed:255.0/255 green:246.0/255 blue:239.0/255 alpha:1];
                        txt2.textColor = [UIColor colorWithRed:254.0/255 green:144.0/255 blue:57.0/255 alpha:1];
                        txt2.backgroundColor = [UIColor colorWithRed:255.0/255 green:246.0/255 blue:239.0/255 alpha:1];
                        txt3.textColor = [UIColor colorWithRed:254.0/255 green:144.0/255 blue:57.0/255 alpha:1];
                        txt3.backgroundColor = [UIColor colorWithRed:255.0/255 green:246.0/255 blue:239.0/255 alpha:1];
                        txt4.textColor = [UIColor colorWithRed:254.0/255 green:144.0/255 blue:57.0/255 alpha:1];
                        txt4.backgroundColor = [UIColor colorWithRed:255.0/255 green:246.0/255 blue:239.0/255 alpha:1];
                        txt5.textColor = [UIColor colorWithRed:254.0/255 green:144.0/255 blue:57.0/255 alpha:1];
                        txt5.backgroundColor = [UIColor colorWithRed:255.0/255 green:246.0/255 blue:239.0/255 alpha:1];
                        
                        for (int i = 0; i < ((NSArray *)idData).count; i++) {
                            MovieLabelModel *model = [((NSArray *)idData) objectAtIndex:i];
                            if (i == 0) {
                                txt.placeholder = @"------";
                                txt.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt setHidden:NO];
                            } else if(i == 1) {
                                txt1.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt1 setHidden:NO];
                            } else if(i == 2) {
                                txt2.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt2 setHidden:NO];
                            } else if(i == 3) {
                                txt3.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt3 setHidden:NO];
                            } else if(i == 4) {
                                txt4.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt4 setHidden:NO];
                            } else if(i == 5) {
                                txt5.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt5 setHidden:NO];
                            }
                        }
                    } else {
                        [txt setPlaceholder:[pArrayPlaceholder objectAtIndex:indexPath.row]];
                    }
                    
                }
                    break;
                    
                case 2:
                {
                    id idData = [pArrayLabel objectAtIndex:indexPath.row];
                    
                    if ([idData isKindOfClass:[NSArray class]])
                    {
                        txt.textColor = [UIColor colorWithRed:254.0/255 green:0.0/255 blue:2.0/255 alpha:1];
                        txt.backgroundColor = [UIColor colorWithRed:255.0/255 green:238.0/255 blue:238.0/255 alpha:1];
                        txt1.textColor = [UIColor colorWithRed:254.0/255 green:0.0/255 blue:2.0/255 alpha:1];
                        txt1.backgroundColor = [UIColor colorWithRed:255.0/255 green:238.0/255 blue:238.0/255 alpha:1];
                        txt2.textColor = [UIColor colorWithRed:254.0/255 green:0.0/255 blue:2.0/255 alpha:1];
                        txt2.backgroundColor = [UIColor colorWithRed:255.0/255 green:238.0/255 blue:238.0/255 alpha:1];
                        txt3.textColor = [UIColor colorWithRed:254.0/255 green:0.0/255 blue:2.0/255 alpha:1];
                        txt3.backgroundColor = [UIColor colorWithRed:255.0/255 green:238.0/255 blue:238.0/255 alpha:1];
                        txt4.textColor = [UIColor colorWithRed:254.0/255 green:0.0/255 blue:2.0/255 alpha:1];
                        txt4.backgroundColor = [UIColor colorWithRed:255.0/255 green:238.0/255 blue:238.0/255 alpha:1];
                        txt5.textColor = [UIColor colorWithRed:254.0/255 green:0.0/255 blue:2.0/255 alpha:1];
                        txt5.backgroundColor = [UIColor colorWithRed:255.0/255 green:238.0/255 blue:238.0/255 alpha:1];
                        
                        for (int i = 0; i < ((NSArray *)idData).count; i++) {
                            MovieLabelModel *model = [((NSArray *)idData) objectAtIndex:i];
                            if (i == 0) {
                                txt.placeholder = @"------";
                                txt.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt setHidden:NO];
                            } else if(i == 1) {
                                txt1.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt1 setHidden:NO];
                            } else if(i == 2) {
                                txt2.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt2 setHidden:NO];
                            } else if(i == 3) {
                                txt3.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt3 setHidden:NO];
                            } else if(i == 4) {
                                txt4.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt4 setHidden:NO];
                            } else if(i == 5) {
                                txt5.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt5 setHidden:NO];
                            }
                        }
                    } else {
                        [txt setPlaceholder:[pArrayPlaceholder objectAtIndex:indexPath.row]];
                    }
                }
                    break;
                    
                case 3:
                {
                    id idData = [pArrayLabel objectAtIndex:indexPath.row];
                    
                    if ([idData isKindOfClass:[NSArray class]])
                    {
                        txt.textColor = [UIColor colorWithRed:195.0/255 green:17.0/255 blue:233.0/255 alpha:1];
                        txt.backgroundColor = [UIColor colorWithRed:252.0/255 green:238.0/255 blue:255.0/255 alpha:1];
                        txt1.textColor = [UIColor colorWithRed:195.0/255 green:17.0/255 blue:233.0/255 alpha:1];
                        txt1.backgroundColor = [UIColor colorWithRed:252.0/255 green:238.0/255 blue:255.0/255 alpha:1];
                        txt2.textColor = [UIColor colorWithRed:195.0/255 green:17.0/255 blue:233.0/255 alpha:1];
                        txt2.backgroundColor = [UIColor colorWithRed:252.0/255 green:238.0/255 blue:255.0/255 alpha:1];
                        txt3.textColor = [UIColor colorWithRed:195.0/255 green:17.0/255 blue:233.0/255 alpha:1];
                        txt3.backgroundColor = [UIColor colorWithRed:252.0/255 green:238.0/255 blue:255.0/255 alpha:1];
                        txt4.textColor = [UIColor colorWithRed:195.0/255 green:17.0/255 blue:233.0/255 alpha:1];
                        txt4.backgroundColor = [UIColor colorWithRed:252.0/255 green:238.0/255 blue:255.0/255 alpha:1];
                        txt5.textColor = [UIColor colorWithRed:195.0/255 green:17.0/255 blue:233.0/255 alpha:1];
                        txt5.backgroundColor = [UIColor colorWithRed:252.0/255 green:238.0/255 blue:255.0/255 alpha:1];
                        
                        for (int i = 0; i < ((NSArray *)idData).count; i++) {
                            MovieLabelModel *model = [((NSArray *)idData) objectAtIndex:i];
                            if (i == 0) {
                                txt.placeholder = @"------";
                                txt.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt setHidden:NO];
                            } else if(i == 1) {
                                txt1.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt1 setHidden:NO];
                            } else if(i == 2) {
                                txt2.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt2 setHidden:NO];
                            } else if(i == 3) {
                                txt3.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt3 setHidden:NO];
                            } else if(i == 4) {
                                txt4.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt4 setHidden:NO];
                            } else if(i == 5) {
                                txt5.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt5 setHidden:NO];
                            }
                        }
                    } else {
                        [txt setPlaceholder:[pArrayPlaceholder objectAtIndex:indexPath.row]];
                    }
                }
                    break;
                    
                case 4:
                {
                    id idData = [pArrayLabel objectAtIndex:indexPath.row];
                    
                    if ([idData isKindOfClass:[NSArray class]])
                    {
                        txt.textColor = [UIColor colorWithRed:0.0/255 green:72.0/255 blue:255.0/255 alpha:1];
                        txt.backgroundColor = [UIColor colorWithRed:239.0/255 green:243.0/255 blue:255.0/255 alpha:1];
                        txt1.textColor = [UIColor colorWithRed:0.0/255 green:72.0/255 blue:255.0/255 alpha:1];
                        txt1.backgroundColor = [UIColor colorWithRed:239.0/255 green:243.0/255 blue:255.0/255 alpha:1];
                        txt2.textColor = [UIColor colorWithRed:0.0/255 green:72.0/255 blue:255.0/255 alpha:1];
                        txt2.backgroundColor = [UIColor colorWithRed:239.0/255 green:243.0/255 blue:255.0/255 alpha:1];
                        txt3.textColor = [UIColor colorWithRed:0.0/255 green:72.0/255 blue:255.0/255 alpha:1];
                        txt3.backgroundColor = [UIColor colorWithRed:239.0/255 green:243.0/255 blue:255.0/255 alpha:1];
                        txt4.textColor = [UIColor colorWithRed:0.0/255 green:72.0/255 blue:255.0/255 alpha:1];
                        txt4.backgroundColor = [UIColor colorWithRed:239.0/255 green:243.0/255 blue:255.0/255 alpha:1];
                        txt5.textColor = [UIColor colorWithRed:0.0/255 green:72.0/255 blue:255.0/255 alpha:1];
                        txt5.backgroundColor = [UIColor colorWithRed:239.0/255 green:243.0/255 blue:255.0/255 alpha:1];
                        
                        for (int i = 0; i < ((NSArray *)idData).count; i++) {
                            MovieLabelModel *model = [((NSArray *)idData) objectAtIndex:i];
                            if (i == 0) {
                                txt.placeholder = @"------";
                                txt.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt setHidden:NO];
                            } else if(i == 1) {
                                txt1.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt1 setHidden:NO];
                            } else if(i == 2) {
                                txt2.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt2 setHidden:NO];
                            } else if(i == 3) {
                                txt3.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt3 setHidden:NO];
                            } else if(i == 4) {
                                txt4.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt4 setHidden:NO];
                            } else if(i == 5) {
                                txt5.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt5 setHidden:NO];
                            }
                        }
                    } else {
                        [txt setPlaceholder:[pArrayPlaceholder objectAtIndex:indexPath.row]];
                    }
                }
                    break;
                    
                case 5:
                {
                    id idData = [pArrayLabel objectAtIndex:indexPath.row];
                    
                    if ([idData isKindOfClass:[NSArray class]])
                    {
                        txt.textColor = [UIColor colorWithRed:2.0/255 green:255.0/255 blue:196.0/255 alpha:1];
                        txt.backgroundColor = [UIColor colorWithRed:239.0/255 green:255.0/255 blue:252.0/255 alpha:1];
                        txt1.textColor = [UIColor colorWithRed:2.0/255 green:255.0/255 blue:196.0/255 alpha:1];
                        txt1.backgroundColor = [UIColor colorWithRed:239.0/255 green:255.0/255 blue:252.0/255 alpha:1];
                        txt2.textColor = [UIColor colorWithRed:2.0/255 green:255.0/255 blue:196.0/255 alpha:1];
                        txt2.backgroundColor = [UIColor colorWithRed:239.0/255 green:255.0/255 blue:252.0/255 alpha:1];
                        txt3.textColor = [UIColor colorWithRed:2.0/255 green:255.0/255 blue:196.0/255 alpha:1];
                        txt3.backgroundColor = [UIColor colorWithRed:239.0/255 green:255.0/255 blue:252.0/255 alpha:1];
                        txt4.textColor = [UIColor colorWithRed:2.0/255 green:255.0/255 blue:196.0/255 alpha:1];
                        txt4.backgroundColor = [UIColor colorWithRed:239.0/255 green:255.0/255 blue:252.0/255 alpha:1];
                        txt5.textColor = [UIColor colorWithRed:2.0/255 green:255.0/255 blue:196.0/255 alpha:1];
                        txt5.backgroundColor = [UIColor colorWithRed:239.0/255 green:255.0/255 blue:252.0/255 alpha:1];
                        
                        for (int i = 0; i < ((NSArray *)idData).count; i++) {
                            MovieLabelModel *model = [((NSArray *)idData) objectAtIndex:i];
                            if (i == 0) {
                                txt.placeholder = @"------";
                                txt.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt setHidden:NO];
                            } else if(i == 1) {
                                txt1.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt1 setHidden:NO];
                            } else if(i == 2) {
                                txt2.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt2 setHidden:NO];
                            } else if(i == 3) {
                                txt3.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt3 setHidden:NO];
                            } else if(i == 4) {
                                txt4.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt4 setHidden:NO];
                            } else if(i == 5) {
                                txt5.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt5 setHidden:NO];
                            }
                        }
                    } else {
                        [txt setPlaceholder:[pArrayPlaceholder objectAtIndex:indexPath.row]];
                    }
                }
                    break;
                    
                case 6:
                {
                    id idData = [pArrayLabel objectAtIndex:indexPath.row];
                    
                    if ([idData isKindOfClass:[NSArray class]])
                    {
                        txt.textColor = [UIColor colorWithRed:.0/255 green:255.0/255 blue:0.0/255 alpha:1];
                        txt.backgroundColor = [UIColor colorWithRed:240.0/255 green:255.0/255 blue:200.0/255 alpha:1];
                        txt1.textColor = [UIColor colorWithRed:.0/255 green:255.0/255 blue:0.0/255 alpha:1];
                        txt1.backgroundColor = [UIColor colorWithRed:240.0/255 green:255.0/255 blue:200.0/255 alpha:1];
                        txt2.textColor = [UIColor colorWithRed:.0/255 green:255.0/255 blue:0.0/255 alpha:1];
                        txt2.backgroundColor = [UIColor colorWithRed:240.0/255 green:255.0/255 blue:200.0/255 alpha:1];
                        txt3.textColor = [UIColor colorWithRed:.0/255 green:255.0/255 blue:0.0/255 alpha:1];
                        txt3.backgroundColor = [UIColor colorWithRed:240.0/255 green:255.0/255 blue:200.0/255 alpha:1];
                        txt4.textColor = [UIColor colorWithRed:.0/255 green:255.0/255 blue:0.0/255 alpha:1];
                        txt4.backgroundColor = [UIColor colorWithRed:240.0/255 green:255.0/255 blue:200.0/255 alpha:1];
                        txt5.textColor = [UIColor colorWithRed:.0/255 green:255.0/255 blue:0.0/255 alpha:1];
                        txt5.backgroundColor = [UIColor colorWithRed:240.0/255 green:255.0/255 blue:200.0/255 alpha:1];
                        
                        for (int i = 0; i < ((NSArray *)idData).count; i++) {
                            MovieLabelModel *model = [((NSArray *)idData) objectAtIndex:i];
                            if (i == 0) {
                                txt.placeholder = @"------";
                                txt.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt setHidden:NO];
                            } else if(i == 1) {
                                txt1.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt1 setHidden:NO];
                            } else if(i == 2) {
                                txt2.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt2 setHidden:NO];
                            } else if(i == 3) {
                                txt3.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt3 setHidden:NO];
                            } else if(i == 4) {
                                txt4.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt4 setHidden:NO];
                            } else if(i == 5) {
                                txt5.text = [NSString stringWithFormat:@"  %@  ", model.name];
                                [txt5 setHidden:NO];
                            }
                        }
                    } else {
                        [txt setPlaceholder:[pArrayPlaceholder objectAtIndex:indexPath.row]];
                    }
                }
                    break;
            }
            
        }
            break;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row)
    {
        case 0:
            return 44;
            
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        {
            id idData = [pArrayLabel objectAtIndex:indexPath.row];
            if ([idData isKindOfClass:[NSArray class]])
            {
                if (((NSArray *)idData).count > 3) {
                    return 80;
                } else {
                    return 44;
                }
            }
        }
    }
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 40;
    }
    return 0.1f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
    [viewHeader setBackgroundColor:[UIColor clearColor]];
    
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, [UIScreen mainScreen].bounds.size.width - 30, 20)];
    [lblTitle setTextColor:[UIColor colorWithRed:153/255.0f green:153/255.0f blue:153/255.0f alpha:1]];
    [lblTitle setFont:[UIFont systemFontOfSize:10]];
    switch (section) {
        case 0:
            [lblTitle setText:@"我想看"];
            break;

        case 1:
            [lblTitle setText:@"屏蔽消息通知,你将不会收到消息提示,但仍会在社交列表里提示"];
            break;
            
        case 2:
            [lblTitle setText:@"兴趣标签(选填)"];
            break;
    }
    [viewHeader addSubview:lblTitle];
    
    return viewHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            pIntSexSelect = (int)indexPath.row;
            [self.pTableView reloadData];
            break;
            
        case 1://不进行任何操作
            break;
            
        case 2: {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            self.curCell = [tableView cellForRowAtIndexPath:indexPath];
            
            switch (indexPath.row) {
                case 0:
                    
                    [self performSegueWithIdentifier:@"idMovieLabelVC" sender:@"星座"];
                    break;
                case 1:
                    [self performSegueWithIdentifier:@"idMovieLabelVC" sender:@"美食"];
                    break;
                case 2:
                    [self performSegueWithIdentifier:@"idMovieLabelVC" sender:@"电影"];
                    break;
                case 3:
                    [self performSegueWithIdentifier:@"idMovieLabelVC" sender:@"演员"];
                    break;
                case 4:
                    [self performSegueWithIdentifier:@"idMovieLabelVC" sender:@"导演"];
                    break;
                case 5:
                    [self performSegueWithIdentifier:@"idMovieLabelVC" sender:@"电影类型"];
                    break;
                case 6:
                    [self performSegueWithIdentifier:@"idMovieLabelVC" sender:@"常去影院"];
                    break;
                    
            }

        }
            break;
    }
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idMovieLabelVC"]) {
        UserMovieLabelVC *nextVC = segue.destinationViewController;
        [nextVC returnText:^(NSString *showText, NSInteger index,NSString *uuid, NSArray *array) {
            UITextField *textFD = [self.curCell viewWithTag:1201];
            if (showText) {
                textFD.text = showText;
                MovieLabelModel *mlm = [[MovieLabelModel alloc] init];
                mlm.name = showText;
                mlm.type = [NSString stringWithFormat:@"%ld", (long)index];
                mlm.uuid = uuid;
                
                NSMutableArray *arr = [NSMutableArray array];
                for (NSDictionary *dict in array) {
                    MovieLabelModel *model = [[MovieLabelModel alloc] init];
                    model.name = [dict objectForKey:@"name"];
                    model.type = [dict objectForKey:@"type"];
                    model.uuid = [dict objectForKey:@"uuid"];
                    [arr addObject:model];
                }
                
                [pArrayLabel replaceObjectAtIndex:index-1 withObject:index==1?mlm:arr];
                [self.pTableView reloadData];

                
                
                
//                textFD.text = showText;
//               [self.pTableView reloadData];
//                [pArrayLabel replaceObjectAtIndex:index-1 withObject:uuid];
//                
//                
//                
//                switch (index-1) {
//                        
//                    case 0:
//                        
//                        textFD.textColor = [UIColor colorWithRed:254.0/255 green:189.0/255 blue:1.0/255 alpha:1];
//                        textFD.backgroundColor = [UIColor colorWithRed:255.0/255 green:250.0/255 blue:238.0/255 alpha:1];
//                        textFD.layer.masksToBounds = YES;
//                        textFD.layer.cornerRadius = 10;
//                        break;
//                    case 1:
//                        textFD.textColor = [UIColor colorWithRed:254.0/255 green:144.0/255 blue:57.0/255 alpha:1];
//                        textFD.backgroundColor = [UIColor colorWithRed:255.0/255 green:246.0/255 blue:239.0/255 alpha:1];
//                        textFD.layer.masksToBounds = YES;
//                        textFD.layer.cornerRadius = 10;
//                        
//                        break;
//                    case 2:
//                        
//                        textFD.textColor = [UIColor colorWithRed:254.0/255 green:0.0/255 blue:2.0/255 alpha:1];
//                        textFD.backgroundColor = [UIColor colorWithRed:255.0/255 green:238.0/255 blue:238.0/255 alpha:1];
//                        textFD.layer.masksToBounds = YES;
//                        textFD.layer.cornerRadius = 10;
//                        break;
//                    case 3:
//                        textFD.textColor = [UIColor colorWithRed:195.0/255 green:17.0/255 blue:233.0/255 alpha:1];
//                        textFD.backgroundColor = [UIColor colorWithRed:252.0/255 green:238.0/255 blue:255.0/255 alpha:1];
//                        textFD.layer.masksToBounds = YES;
//                        textFD.layer.cornerRadius = 10;
//                        break;
//                    case 4:
//                        textFD.textColor = [UIColor colorWithRed:0.0/255 green:72.0/255 blue:255.0/255 alpha:1];
//                        textFD.backgroundColor = [UIColor colorWithRed:239.0/255 green:243.0/255 blue:255.0/255 alpha:1];
//                        textFD.layer.masksToBounds = YES;
//                        textFD.layer.cornerRadius = 10;
//                        break;
//                    case 5:
//                        textFD.textColor = [UIColor colorWithRed:2.0/255 green:255.0/255 blue:196.0/255 alpha:1];
//                        textFD.backgroundColor = [UIColor colorWithRed:239.0/255 green:255.0/255 blue:252.0/255 alpha:1];
//                        textFD.layer.masksToBounds = YES;
//                        textFD.layer.cornerRadius = 10;
//                        break;
//                    case 6:
//                        textFD.textColor = [UIColor colorWithRed:.0/255 green:255.0/255 blue:0.0/255 alpha:1];
//                        textFD.backgroundColor = [UIColor colorWithRed:240.0/255 green:255.0/255 blue:200.0/255 alpha:1];
//                        textFD.layer.masksToBounds = YES;
//                        textFD.layer.cornerRadius = 10;
//                        break;
//                        
//                        
//                        
//                }
            }

        }];
        nextVC.pNavTitle = sender;
    }
}


@end
